%macro prnt (program=, disname=, idisname=,ds_list=);
Data adae;
   set adam.adae;
   where saffl='Y' and TRTEMFL='Y' and trtan in (1 2 3 4 5 6);;
   keep usubjid saffl trtemfl trta aebodsys aebodsys aedecod aoccfl;
   run;

   proc freq data=adae noprint;
   tables trta*aoccfl/out=check;
   where aoccfl ne '';
   run;
***Read in ADSL Data***;
data adsl;
   set adam.adsl(where=(saffl='Y'));
   part1=scan(actarmcd, 1, '/');
   part2=scan(actarmcd, 2, '/');
   trt1=substr(part1, 1, 1);
   trt2=substr(part1, 2, 1);
   trt3=substr(part1, 3, 1);
   trt4=substr(part2, 1, 1);
   trt5=substr(part2, 2, 1);
   trt6=substr(part2, 3, 1);
   if trt1='A' or TRT2='A' or TRT3='A' or trt4='A' or TRT5='A' or TRT6='A' then trta='A'; 
output;
  if trt1='B' or TRT2='B' or TRT3='B' or trt4='B' or TRT5='B' or TRT6='B' then trta='B'; 
output;  
if trt1='C' or TRT2='C' or TRT3='C' or trt4='C' or TRT5='C' or TRT6='C' then trta='C'; 
output;
run;

data adsl;
   set adsl;
   where trta ne '';run;
/*proc sort data=adsl nodupkey dupout=dum1;*/
/*   by usubjid trta;run; */
proc sql;
   create table pct as 
   select count(distinct usubjid) as bign, trta
   from adsl
   group by trta;
quit;
data pct;
   set pct;
   if trta='A' then ntrt=1;
   if trta='B' then ntrt=2;
   if trta='C' then ntrt=3;run;
data _null_ ;
set pct;
call symput("trt"||strip(put(ntrt, best.)), strip(put(bign, 8.)));
run;
%put &trt1 &trt2 &trt3;
proc sql;
   create table row1 as 
   select trta, count(distinct usubjid) as count, "ANY SUPAE" as label, 1 as ord
   from adae
   group by trta
   outer union corr

    select trta,aebodsys, count(distinct usubjid) as count, "  "||aebodsys as label,2 as ord 
   from adae
   group by trta, aebodsys
   outer union corr

   select trta,aebodsys, aedecod, count(distinct usubjid) as count, "    "||aedecod as label, 3 as ord 
   from adae
   group by trta, aebodsys, aedecod;
   quit;
***Nuber of subjects by trt***;
proc sort data=adae out=tot nodupkey;
   by trta usubjid;
run;

proc freq data=tot ;
tables trta/out=freq1;run;

proc sort data=adae out=soc nodupkey;
   by trta usubjid aebodsys;
run;
proc freq data=soc ;
tables trta*aebodsys/out=freq2;run;
proc sort data=adae out=pt nodupkey;
   by trta usubjid aebodsys aedecod;
run;
proc freq data=pt ;
tables trta*aebodsys*aedecod/out=freq3;run;
data total;
   set freq1(in=a) freq2(in=b) freq3(in=c);
   if a then order=1;
   if b then order=2;
   if c then order=3;
run;
proc sort data=total;
   by trta;
run;

proc sort data=pct;
   by trta;
run;
***Nuber of events by trt***;

proc sort data=adae /*nodupkey*/ out=eve ;
   by usubjid trta aebodsys aedecod;
run;
/*
proc sort data=adae out=eve ;
   by trta ;
run;
*/
proc freq data=eve ;
tables trta/out=cnt1;run;

proc sort data=eve out=soc_eve;
   by trta aebodsys;
run;

proc freq data=soc_eve ;
tables trta*aebodsys/out=cnt2;run;

proc sort data=eve out=pt_eve ;
   by trta aebodsys aedecod;
run;

proc freq data=pt_eve ;
tables trta*aebodsys*aedecod/out=cnt3;run;

data total_eve(rename=(count=cnt));
   set cnt1(in=a) cnt2(in=b) cnt3(in=c);
   if a then order=1;
   if b then order=2;
   if c then order=3;
run;
proc sort data=total_eve ;
   by trta order aebodsys aedecod ;
run;
proc sort data=total ;
   by  trta order aebodsys aedecod ;
run;

data pre_table;
merge total total_eve;
by  trta order aebodsys aedecod ;
run;

data table;
   merge pre_table pct;
   by trta;
   if cnt not in( . 0) then stats='   '||put(count, 4.)||' ('||put((count/bign*100), 5.1)||')' ||'  '||put(cnt, 4.);
run;

proc sort data=table nodupkey dupout=dum;
   by order trta aebodsys aedecod;

run;
proc sort data=table ;
   by order aebodsys aedecod;

run;
proc transpose data=table out=table1;
   by order aebodsys aedecod;
   var stats;
   id trta;
run;

proc sort;
by aebodsys aedecod;
run;

data final(drop=order );
   length col1 $200;
   set table1;
   by  aebodsys aedecod;
   if aedecod ^='' then col1="  "||strip(aedecod);
   if col1='' and aebodsys ne '' then col1=strip(aebodsys);
   if col1='' then col1="Any SPUEAE";
   page=ceil(_n_/21);
run;

data final;
set final;
array lp(3) $ a b c;
do i=1 to 3;
if lp(i)='' then lp(i)="      0";
end;
run;

***Printing using proc report;
******************************************************************************************************;
title3 "Table 14.3.1.2.3 (Continued)";

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T14.3.1.2.3.AE.rtf";


%open_rtf;
%empty_ds_fix(final);

title4 "Summary of Study Product Use-Emergent Adverse Events by System Organ Class and MedDRA Preferred Term  Overall";
title5 "(Safety Population)";
%footer1(foot1=1, ds_list=&ds_list., Program=&program.);
footnote3 j=l "Note(s): For the subject counts, each Study Product Use-Emergent adverse event is counted only once for each subject within each System Organ Class and";
footnote4 j=l "MedDRA Preferred Term.";
proc report data=final nowd split='~' missing;
 column page aebodsys ("\line System Organ Class~    MedDRA Preferred Term"  col1)
       ("\line Study Product A~(N=&trt1)" A) ("\line Study Product B~(N=&trt2)" B) (" \line Study Product C~(N=&trt3)" C);
  
   

 define page         / " " order order=internal noprint;
 define aebodsys      / " " order order=internal noprint;
/* define sort1        / " " order descending order=internal noprint;*/
/* define level1_label / " " order order=internal noprint;*/
/* define level2       / " " order order=internal noprint;*/
/* define sort2        / " " order descending order=internal noprint;*/
 define col1 /display "~ ~ " order=internal style(column)=[cellwidth=3.0in just=left] style(header)=[just=left];
define A /display "~Subjects   Events~[n(%)]  [n]" order=internal style(column)=[cellwidth=2.0in just=left] style(header)=[just=center];
define B /display "~Subjects   Events~[n(%)]  [n]" order=internal style(column)=[cellwidth=2.0in just=left] style(header)=[just=center];
define C /display "~Subjects   Events~[n(%)]  [n]" order=internal style(column)=[cellwidth=2.0in just=left] style(header)=[just=center];



 break after page  / page;

 
 compute before aebodsys;
  line " ";
 endcomp;

run;

%close_rtf;
%mend prnt;
%prnt(program=T14_03_01_02_03_AE, disname=14.3.1.2.3, idisname=14.3.1.2.3, ds_list=ADAE );

%clrw;





